package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyStores;
import com.yjt.fresh.api.ApiGetHistoryCompanies;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanStore;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.widget.ClearEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.nfc.tech.MifareUltralight.PAGE_SIZE;

/**
 * Created by UPC on 2018/8/14.
 */

public class AtyHistoryCompanies extends BaseSimpleActivity {
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.rvAppAtyHistoryCompanies)
    RecyclerView mRv;
    @BindView(R.id.srlAppAtyHistoryCompanies)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.etAppAtyHistoryCompanies)
    ClearEditText mEtSearch;
    @BindView(R.id.tvAppAtyHistoryCompaniesCancel)
    TextView mTvCancel;
    private AdpRvAtyStores mAdpRvAtyStores;
    private int mNextRequestPage = 1;
    private boolean isLoadMore;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_hitory_companies;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        initRv();
        getData();
        initClick();
    }

    private void initClick() {
        mAdpRvAtyStores.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext,AtyHistoryVideos.class);
                intent.putExtra(MyConstants.USER_ID,mAdpRvAtyStores.getItem(position).getSTORE_ID());
                intent.putExtra(MyConstants.CONTENT,mAdpRvAtyStores.getItem(position).getCONTENT());
                intent.putExtra(MyConstants.TITLE,mAdpRvAtyStores.getItem(position).getSTORE_NAME());
                startActivity(intent);
            }
        });
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });

        mAdpRvAtyStores.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        }, mRv);

        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    mNextRequestPage = 1;
                    isLoadMore = false;
                    getData();
                    return true;
                }
                return false;
            }
        });
    }

    HttpOnNextListener<List<BeanStore>> mCompaniesListener = new HttpOnNextListener<List<BeanStore>>() {
        @Override
        public void onNext(List<BeanStore> beanStores) {

            mAdpRvAtyStores.setEmptyView(R.layout.view_empty);
            if (isLoadMore) {
                setData(false, beanStores);
            } else {
                setData(true, beanStores);
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void setData(boolean isRefresh, List<BeanStore> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpRvAtyStores.setNewData(data);
        } else {
            if (size > 0) {
                mAdpRvAtyStores.addData(data);
                mAdpRvAtyStores.loadMoreComplete();
            }else {
                mAdpRvAtyStores.loadMoreEnd(true);
            }
        }
    }

    private void getData() {
        ApiGetHistoryCompanies apiGetHistoryCompanies = new ApiGetHistoryCompanies(mCompaniesListener, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("currentPage", mNextRequestPage);
        if (!TextUtils.isEmpty(mEtSearch.getText().toString())) {
            map.put("keywords", mEtSearch.getText().toString());
        }
        apiGetHistoryCompanies.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetHistoryCompanies);
    }

    private void initRv() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        List<BeanStore> list = new ArrayList<>();
        mAdpRvAtyStores = new AdpRvAtyStores(R.layout.app_recycle_item_stores, list);
        mRv.setAdapter(mAdpRvAtyStores);
    }


}
